package ru.javadevelop.vegetarian.common;

import org.joda.time.DateTime;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Для полноценной работы класса нужна библиотека jodaTime
 */
public class StringUtils {
    public static  String convertStreamToString(InputStream is)
    {
        BufferedReader reader = new BufferedReader(
                new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();

        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }
    //******************************************************************************************************************
    // Использование:
    // Date date = parseDate("19/05/2009", "dd/MM/yyyy");
    public static Date parseDate(String date, String format) throws ParseException
    {
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        return formatter.parse(date);
    }
    //******************************************************************************************************************
    public static Date parseDate8601(String date)
    {
        DateTime dt = new DateTime(date);
        return dt.toDate();
    }
}
